package wfmanage.action;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

import com.util.MyLog;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.util.DB;

import edu.wfmanage.models.Lookup;
import edu.wfmanage.models.User;

/**
 * 
 * @author Satya Deepthi Bhagi
 */

public class Login extends ActionSupport {
    
	private static final long serialVersionUID = 1L;
	private String commandButton = "";
	private User user;
	public String getCommandButton() {
		return commandButton;
	}

	public void setCommandButton(String commandButton) {
		this.commandButton = commandButton;
	}
	
	private String username="";
	private String user_id="";
	private String email_id="";
	private String role="";
    private int LTALimit;
    private String submit;
	
    
	

	public String getSubmit() {
		return submit;
	}

	public void setSubmit(String submit) {
		this.submit = submit;
	}

	public int getLTALimit() {
		return LTALimit;
	}

	public void setLTALimit(int lTALimit) {
		LTALimit = lTALimit;
	}

	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}

	public String getEmail_id() {
		return email_id;
	}

	public void setEmail_id(String email_id) {
		this.email_id = email_id;
	}

	public String getUser_id() {
		return user_id;
	}

	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	private String password="";
   

	public String execute() {
		MyLog.log("in Login.execute() with commandButton = " + 
    	        this.commandButton);
		MyLog.log("in Login.execute() with commandButton = " + 
    	        this.commandButton);
        if (this.username.isEmpty()) {
			// first time screen
		    return "initial_entry";
        } 
        MyLog.log("Username is:" +username+  "password is: "+password);
        Connection con = DB.getConnection();
        Statement stm = null;
		String sql = "SELECT * FROM EMPLOYEE";
		sql += " WHERE USER_NAME = '" + username + "'";
		sql += " AND password = '" + password+ "'";
		ResultSet rs = null;
		try {
			stm = con.createStatement();
			rs = stm.executeQuery(sql);
			while (rs.next()) {
				user_id=rs.getString("USER_ID");
				email_id =rs.getString("EMAIL_ID");
				role=rs.getString("EMP_ROLE");
				LTALimit=rs.getInt("LTA_LIMIT");
				setUser_id(user_id);
				setEmail_id(email_id);
				User user=new User();
				user.setEmailid(email_id);
				user.setPassword(password);
				user.setUserid(user_id);
				user.setLTALimit(LTALimit);
				user.setUsername(username);
				user.setRole(role);
				Map<String, Object> session = ActionContext.getContext().getSession();
				
				if(session.get("user") != null && session.get("user") != "")
					user = (User) session.get("user");
				if (user == null) {
					user = new User();
				}
				session.put("user", user);
				
			}
			//Sravvani code
			Lookup check_iscurrent = new Lookup();
			check_iscurrent=Lookup.findOne("user", "where user_name='" + username + "' and password ='" + password + "' and is_current=1;");
			int is_current=check_iscurrent.getIs_current();
			MyLog.log("is_current"+is_current);
			if(role.equalsIgnoreCase("Admin") && is_current==1){
				return "admin_success";
			}
		// If the combination entered by the user does not exist in the user table, the ResultSet will be empty. Redirect the user back to the login page
			if (user_id==null|user_id=="") {
				addActionError(getText("Incorrect user id and / or password!"));
		        return "error";
			}
			
		

		} catch (SQLException e) {
			MyLog.log("Error while checking credentials from database"+e);
		}
		return "success";

    }
 
    
    public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getPassword() {
        return password;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
   
}
